package com.hisense.ovcloud.apicontrol.service;

import com.hisense.ovcloud.apicontrol.errors.ResForGW;
import com.hisense.ovcloud.apicontrol.errors.SystemError;

import java.util.LinkedHashMap;

public class BaseAGService {
    public ResForGW setErrorResponse(ResForGW res, SystemError error) {
        res.setResultCode(error.value());
        res.setDesc(error.desc());
        res.setSe(error);
        res.setStopForwarding(true);
        return res;
    }

    public String getFromParametersMap(LinkedHashMap<String, Object> parametersMap, String key){
        if(parametersMap.isEmpty()){
            return null;
        }else{
            if(parametersMap.containsKey(key)){
                Object value = parametersMap.get(key);
                if (value != null) {
                    return value.toString();
                }else{
                    return null;
                }
            }else{
                return null;
            }
        }
    }

    public <T> T safeGetValue(LinkedHashMap<String, Object> map, String key, Class<T> targetType) {
        Object value = map.get(key);
        if (value == null) {
            return null;
        }
        if (targetType.isInstance(value)) {
            return targetType.cast(value);
        } else {
            throw new ClassCastException("Value for key '" + key + "' is not of type " + targetType.getName());
        }
    }
}
